##geo_t_tests.r
###analyze each city by a t test for difference

cities = levels(unique(dat$index.city))  ##unique cities in the data set

public.mat = data.frame(matrix(nrow = length(cities), ncol = 6))
dictator.mat = data.frame(matrix(nrow = length(cities), ncol = 6))
task.mat = data.frame(matrix(nrow = length(cities), ncol = 6))

for(i in 1:length(cities)){  ###cycle through cities and test on each
	city = cities[i]
	public.test = t.test(dat$public.ingroup.preference[dat$index.city == city]) ##use in out difference here
	public.mat[i,1] = as.character(city)
	public.mat[i,2] = public.test$estimate
	public.mat[i,3] = public.test$statistic
	public.mat[i,4] = public.test$p.value
	public.mat[i,5] = public.test$conf.int[1]
	public.mat[i,6] = public.test$conf.int[2]
	
	dictator.test = t.test(dat$in.out.diff.dictator[dat$index.city == city])
	dictator.mat[i,1] = as.character(city)
	dictator.mat[i,2] = dictator.test$estimate/20
	dictator.mat[i,3] = dictator.test$statistic
	dictator.mat[i,4] = dictator.test$p.value
	dictator.mat[i,5] = dictator.test$conf.int[1]/20
	dictator.mat[i,6] = dictator.test$conf.int[2]/20

	
	task.test = t.test(dat$ingroup.task,dat$outgroup.task[dat$index.city == city])
	task.mat[i,1] = as.character(city)
  task.mat[i,2] = task.test$estimate[1]-task.test$estimate[2]
	task.mat[i,3] = task.test$statistic
	task.mat[i,4] = task.test$p.value
	task.mat[i,5] = task.test$conf.int[1]
	task.mat[i,6] = task.test$conf.int[2]
	}

colnames(public.mat) = c('index.city','estimate','t.value','p.value','conf.min','conf.max')

colnames(dictator.mat) = c('index.city','estimate','t.value','p.value','conf.min','conf.max')

colnames(task.mat) = c('index.city','estimate','t.value','p.value','conf.min','conf.max')



##plots
##sort by segregation, then sort matrices on same order
segs = data.cities[,c('index.city','dissim.yeshiva')]

dictator.mat = merge(dictator.mat,segs, all.x = T, all.y = F)
dictator.mat = dictator.mat[order(dictator.mat$dissim.yeshiva),]
task.mat = merge(task.mat,segs, all.x = T, all.y = F)
task.mat = task.mat[order(task.mat$dissim.yeshiva),]
public.mat = merge(public.mat,segs, all.x = T, all.y = F)
public.mat = public.mat[order(public.mat$dissim.yeshiva),]

city.labs = gsub("_"," ",dictator.mat$index.city)
city.labs = capwords(city.labs)

##replace city labs to represent quarters
city.labs[city.labs == 'Neve Yaakov'] = 'Jerusalem Quarter 1'
city.labs[city.labs == 'Ramat Shlomo'] = 'Jerusalem Quarter 4'
city.labs[city.labs == 'Jerusalem Center'] = 'Jerusalem Quarter 8'
city.labs[city.labs == 'Kiryat Yovel'] = 'Jerusalem Quarter 11'


colors = brewer.pal(5,'Set1')[1:5]
text.size = .75
line.width = 1.25
pdf('output/appendix/Figure_A4.pdf',
	height = 9,
	width = 6.95)
par(las = 1)
par(mar = c(3,1,1,1))
par(bty = 'n')
plot(rep(.5,20),
	c(1:20),
	type = 'n',
	xlim = c(-.75,.75),
	yaxt = 'n',
	xaxt = 'n',
	ylab = '',
	xlab = '',
	)
abline(v = 0,
	col = 'gray',
	lwd = 2.5)
axis(side = 1,
		at = seq(from = -.5, to = .8, by = .2)
		)

##find mins of each city group for label placing
mins = cbind(dictator.mat[,'conf.min'],task.mat[,'conf.min'],public.mat[,'conf.min'])
min.mins = apply(mins, 1, min, na.rm = T)

text(y = c(1:20),
	x = min.mins,
	labels = city.labs,
	cex.axis = .85,
	col = colors,
	pos = 2
	)


##plot other regarding preferences	
for(i in 1:20){
	lines(x = dictator.mat[i,c('conf.min','conf.max')],
		y = rep(i,2)+.15,
		lwd = line.width,
		lty = 3)
	}
text(dictator.mat$estimate,
	1:20+.15,
	rep('R',2),
	col = colors,
	cex = text.size)

##plot technology
for(i in 1:20){
	lines(x = task.mat[i,c('conf.min','conf.max')],
		y = rep(i,2),
		lwd = line.width,
		lty = 3)
	}
text(task.mat$estimate,  ##rescale these t
	1:20,
	rep('E',20),
	col = colors,
	cex = text.size)

##plot cooperating
for(i in 1:20){
	lines(x = public.mat[i,c('conf.min','conf.max')],
		y = rep(i,2) - .15,
		lwd = line.width,
		lty = 3)
	}
text(public.mat$estimate,
	1:20 - .15,
	rep('S',20),
	col = colors,
	cex = text.size)
	dev.off()

	
###CITY SUBSET
use.cities = c('jerusalem_center','bnei_brak', 'kiryat_gat', 'kiryat_yovel')

use.dictator.mat = dictator.mat[dictator.mat$index.city %in% use.cities,]
use.dictator.mat = rbind(use.dictator.mat[2,],use.dictator.mat[4,],use.dictator.mat[1,],use.dictator.mat[3,])

use.task.mat = task.mat[task.mat$index.city %in% use.cities,]
use.task.mat = rbind(use.task.mat[2,],use.task.mat[4,],use.task.mat[1,],use.task.mat[3,])

use.public.mat = public.mat[public.mat$index.city %in% use.cities,]
use.public.mat = rbind(use.public.mat[2,],use.public.mat[4,],use.public.mat[1,],use.public.mat[3,])

city.labs = c("Bnei Brak", "Jerusalem Quarter 8","Jerusalem Quarter 11", "Kiryat Gat")

colors = brewer.pal(4,'Set1')[1:4]
	
text.size = 2
line.width = 2

pdf('output/Figure_3.pdf',
	height = 9,
	width = 9)
par(bty = 'n')
par(las = 1)
par(mar = c(3,1,1,1))
	plot(rep(.5,4),
		c(1:4),
		type = 'n',
		xlim = c(-.75,.75),
		ylim = c(.5,4.5),
		yaxt = 'n',
		xaxt = 'n',
		ylab = '',
		xlab = '',
		)
	abline(v = 0,
		col = 'gray',
		lwd = 2.5)
axis(side = 1,
		at = seq(from = -.5, to = .8, by = .2)
		)

	##find mins of each city group for label placing
	mins = cbind(use.dictator.mat[,'conf.min'],use.task.mat[,'conf.min'],use.public.mat[,'conf.min'])
	min.mins = apply(mins, 1, min, na.rm = T)

	text(y = c(1:4),
		x = min.mins,
		labels = city.labs,
		cex = 1.75,
		col = colors,
		pos = 2
		)


	##plot other regarding preferences	
	for(i in 1:4){
		lines(x = use.dictator.mat[i,c('conf.min','conf.max')],
			y = rep(i,2)+.15,
			lwd = line.width,
			lty = 3)
		}
	text(use.dictator.mat$estimate,
		1:4+.15,
		rep('R',2),
		col = colors,
		cex = text.size)

	##plot technology
	for(i in 1:4){
		lines(x = use.task.mat[i,c('conf.min','conf.max')],
			y = rep(i,2),
			lwd = line.width,
			lty = 3)
		}
	text(use.task.mat$estimate,  ##rescale these t
		1:4,
		rep('E',4),
		col = colors,
		cex = text.size)

	##plot cooperating
	for(i in 1:4){
		lines(x = use.public.mat[i,c('conf.min','conf.max')],
			y = rep(i,2) - .15,
			lwd = line.width,
			lty = 3)
		}
	text(use.public.mat$estimate,
		1:4 - .15,
		rep('S',4),
		col = colors,
		cex = text.size)
	dev.off()